
// 首页业务
    const res = localStorage.getItem('name')
    document.querySelector('.username').innerHTML = res


checkLogin()
logout()

const line = document.querySelector('#line')

async function getDate() {
    const token = localStorage.getItem('token')
    const { data: {
        overview, year, salaryData, groupData, provinceData
    } } = await axios('/dashboard')
    // url: '/dashboard',
    // headers: {
    //     Authorization: token
    // }
    renderOverview(overview)
    renderyear(year)
    rendersalaryData(salaryData)
    rendergroupData(groupData)
    salary(salaryData)
    renderprovinceData(provinceData


    )
}
getDate()

function renderOverview(overview) {
    document.querySelector('.salary').innerHTML = overview.salary
    document.querySelector('.student_count').innerHTML = overview.student_count
    document.querySelector('.age').innerHTML = overview.age
    document.querySelector('.group_count').innerHTML = overview.group_count
}

function renderyear(year) {
    const myChart = echarts.init(document.querySelector('#line'))
    const option = {
        title: {
            text: '2024全学科薪资走向',
            left: 10,
            top: 10
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: year.map(item => item.month)
        },
        yAxis: {
            type: 'value'

        },
        series: [
            {
                data: year.map(item => item.salary),
                type: 'line',
                areaStyle: {
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [{
                            offset: 0, color: 'red' // 0% 处的颜色
                        }, {
                            offset: 1, color: 'blue' // 100% 处的颜色
                        }],

                    },
                },

                smooth: true,
                lineStyle: {
                    width: 6,
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 1,
                        y2: 0,
                        colorStops: [{
                            offset: 0, color: '#cee8fc' // 0% 处的颜色
                        }, {
                            offset: 1, color: '#5577ff' // 100% 处的颜色
                        }],
                        global: false // 缺省为 false
                    }
                }
            }
        ]
    };
    myChart.setOption(option)
}

function rendersalaryData(salaryData) {
    const salary = document.querySelector('#salary')
    const myChart = echarts.init(salary)
    const option = {
        tooltip: {
            trigger: 'item',
            data: ['班级薪资分布']
        },
        title: {
            text: '班级薪资分布',
            left: 10,
            top: 10
        },
        legend: {
            bottom: '5%',
            left: 'center'
        },
        series: [
            {
                name: 'Access From',
                type: 'pie',
                radius: ['40%', '50%'],
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                labelLine: {
                    show: false
                },
                data: salaryData.map(item => {
                    return { value: item.g_count + item.b_count, name: item.label }
                })
            }
        ],
        color: ['#f0a645', '#6295f8', '#63b9f4', '#6ad09e']
    };
    myChart.setOption(option)
}
function rendergroupData(groupData) {
    const myChart = echarts.init(document.querySelector('#lines'))
    const btns = document.querySelector('#btns')
    const option = {
        xAxis: {
            type: 'category',
            data: groupData[1].map(item => {
                return item.name
            }),
            axisLine: {
                lineStyle: {
                    color: '#999',
                    type: 'dashed'
                }
            }
        },
        yAxis: {
            type: 'value',
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            }
        },
        series: [
            {
                data: groupData[1].map(item => {
                    return item.hope_salary
                }),
                type: 'bar'
            },
            {
                data: groupData[1].map(item => {
                    return item.salary
                }),
                type: 'bar'
            }
        ]
    };
    myChart.setOption(option)
    btns.addEventListener('click', function (e) {
        if (e.target.classList.contains('btn')) {
            document.querySelector('.btn-blue').classList.remove('btn-blue')
            e.target.classList.add('btn-blue')
            const option = {
                xAxis: {
                    type: 'category',
                    data: groupData[e.target.innerHTML].map(item => {
                        return item.name
                    }),
                    axisLine: {
                        lineStyle: {
                            color: '#999',
                            type: 'dashed'
                        }
                    }
                },
                yAxis: {
                    type: 'value',
                    splitLine: {
                        lineStyle: {
                            type: 'dashed'
                        }
                    }
                },
                series: [
                    {
                        data: groupData[e.target.innerHTML].map(item => {
                            return item.hope_salary
                        }),
                        type: 'bar'
                    },
                    {
                        data: groupData[e.target.innerHTML].map(item => {
                            return item.salary
                        }),
                        type: 'bar'
                    }
                ]
            };
            myChart.setOption(option)
        }
    })
}
function renderprovinceData(provinceData) {
    const myChart = echarts.init(document.querySelector('#map'))
    const dataList = [
        { name: '南海诸岛', value: 0 },
        { name: '北京', value: 0 },
        { name: '天津', value: 0 },
        { name: '上海', value: 0 },
        { name: '重庆', value: 0 },
        { name: '河北', value: 0 },
        { name: '河南', value: 0 },
        { name: '云南', value: 0 },
        { name: '辽宁', value: 0 },
        { name: '黑龙江', value: 0 },
        { name: '湖南', value: 0 },
        { name: '安徽', value: 0 },
        { name: '山东', value: 0 },
        { name: '新疆', value: 0 },
        { name: '江苏', value: 0 },
        { name: '浙江', value: 0 },
        { name: '江西', value: 0 },
        { name: '湖北', value: 0 },
        { name: '广西', value: 0 },
        { name: '甘肃', value: 0 },
        { name: '山西', value: 0 },
        { name: '内蒙古', value: 0 },
        { name: '陕西', value: 0 },
        { name: '吉林', value: 0 },
        { name: '福建', value: 0 },
        { name: '贵州', value: 0 },
        { name: '广东', value: 0 },
        { name: '青海', value: 0 },
        { name: '西藏', value: 0 },
        { name: '四川', value: 0 },
        { name: '宁夏', value: 0 },
        { name: '海南', value: 0 },
        { name: '台湾', value: 0 },
        { name: '香港', value: 0 },
        { name: '澳门', value: 0 },
    ]
    dataList.forEach(item => {
        const res = provinceData.find(ele => ele.name.includes(item.name))
        if (res) {
            item.value = res.value
        }
    })
    const option = {
        title: {
            text: '籍贯分布',
            top: 10,
            left: 10,
            textStyle: {
                fontSize: 16,
            },
        },
        tooltip: {
            trigger: 'item',
            formatter: '{b}: {c} 位学员',
            borderColor: 'transparent',
            backgroundColor: 'rgba(0,0,0,0.5)',
            textStyle: {
                color: '#fff',
            },
        },
        visualMap: {
            min: 0,
            max: 6,
            left: 'left',
            bottom: '20',
            text: ['6', '0'],
            inRange: {
                color: ['#ffffff', '#0075F0'],
            },
            show: true,
            left: 40,
        },
        geo: {
            map: 'china',
            roam: false,
            zoom: 1.0,
            label: {
                normal: {
                    show: true,
                    fontSize: '10',
                    color: 'rgba(0,0,0,0.7)',
                },
            },
            itemStyle: {
                normal: {
                    borderColor: 'rgba(0, 0, 0, 0.2)',
                    color: '#e0ffff',
                },
                emphasis: {
                    areaColor: '#34D39A',
                    shadowOffsetX: 0,
                    shadowOffsetY: 0,
                    shadowBlur: 20,
                    borderWidth: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)',
                },
            },
        },
        series: [
            {
                name: '籍贯分布',
                type: 'map',
                geoIndex: 0,
                data: dataList,
            },
        ],
    }
    myChart.setOption(option)
}

function salary(salary) {
    const myChart = echarts.init(document.querySelector('#gender'))
    var datas = [salary.map(item => {
        return { name: item.label, value: item.b_count }
    }),
    salary.map(item => {
        return { name: item.label, value: item.g_count }
    })]

    option = {
        title: [{
            text: '男女薪资分布',
            left: 10,
            textStyle: {
                color: '#999',
                fontWeight: 'normal',
                fontSize: 14
            }
        },
        {
            text: '男',
            left: 'center',
            top: '25%',
            textStyle: {
                color: '#999',
                fontWeight: 'normal',
                fontSize: 14
            }
        },
        {
            text: '女',
            left: 'center',
            top: '65%',
            textStyle: {
                color: '#999',
                fontWeight: 'normal',
                fontSize: 14
            }
        },],
        tooltip: {
            trigger: 'item'
        },
        series: datas.map(function (data, idx) {
            var top = idx * 40 + 10;
            return {
                type: 'pie',
                radius: [45, 60],
                top: top + '%',
                height: '33.33%',
                left: 'center',
                width: 400,
                itemStyle: {
                    borderColor: '#fff',
                    borderWidth: 1
                },
                data: data,
                color: ['#fb9600', '#1890fb', '#00bcfb', '#00da8f']
            };
        })
    };
    myChart.setOption(option)
}
